<?php

function load_database_fields($schema)
{
	/* 读取数据库的schema,返回一个关联数组如下
	 * array(array("engname"=>"nishejijin","chiname"=>"拟设基金","type"=>"str"),)
	 */
	$file_path = drupal_get_path("module", "manager_resume")."/".$schema;
	$fp = fopen($file_path, "r");
	$fields = array();
	while (!feof($fp))
	{
		$line=fgets($fp);
		$line = chop($line);
		$sep = explode("|", $line);
		$fields[] = array("engname"=>$sep[0],"chiname"=>$sep[1],"type"=>$sep[2]);
	}
	fclose($fp);
	return $fields;
}

function get_resume_by_id($id)
{
	$query = "SELECT * FROM {manager_resume} WHERE id = ?";
	$query_result = db_query($query,array($id));
	$result_values = $query_result->fetchAssoc();
	return $result_values;
}

function get_gudong_resume_by_id($id)
{
    $query = "SELECT * FROM {gudong_resume} WHERE id = ?";
    $query_result = db_query($query,array($id));
    $result_values = $query_result->fetchAssoc();
    return $result_values;
}
function get_resume_by_jijin_id_and_gudong_seq($jinjin_id,$gudong_seq)
{
    $query = "SELECT * FROM {gexpertmanage/chooseudong_resume} WHERE jijin_id = ? AND gudong_seq = ?";
    $query_result = db_query($query,array($jinjin_id,$gudong_seq));
    $result_values = $query_result->fetchAssoc();
    return $result_values;
}
function get_sub_fund_money_record_by_id($id)
{
	$query = "SELECT * FROM {sub_fund_money} WHERE id = ?";
	$query_result = db_query($query,array($id));
	$result_values = $query_result->fetchAssoc();
	return $result_values;
}
function get_registered_name_by_id($id)
{
	/*
	 * 读取数据库，返回$id对应的数据库的记录，以关联数组形式存储。
	 */
	$query = "SELECT * FROM {fund_basic_information} WHERE id = ?";
	$query_result = db_query($query,array($id));
	$result_values = $query_result->fetchAssoc();
	return $result_values["registered_name"];
}

function get_jijin_id_by_user_id($user_id)
{
	$query = "SELECT * FROM {fund_basic_information} WHERE user_id = ?";
	$query_result = db_query($query,array($user_id));
	$result_values = $query_result->fetchAssoc();
	return $result_values["id"];
}
function get_fund_basic_information_by_id($fund_id)
{
	$query = "SELECT * FROM {fund_basic_information} WHERE id = ?";
	$query_result = db_query($query,array($fund_id));
	$result_values = $query_result->fetchAssoc();
	return $result_values;
}
function get_fund_co_info_by_id($fund_id)
{
	$query = "SELECT * FROM {fund_co_info} WHERE jijin_id = ?";
	$query_result = db_query($query,array($fund_id));
	$result_values = $query_result->fetchAssoc();
	return $result_values;
}
function manager_resume_create_page() {
	$html = array("#markup" => "");
	$d_form = drupal_get_form("manager_resume_create_form");
	$html["#markup"] .= drupal_render($d_form);
	return $html;
}
function gudong_resume_create_page($seq) {
    if ($seq < 0 or $seq > 4) {
        drupal_goto('manager_resume/gudong/createfail');
    }
    $html = array("#markup" => "");
    $d_form = drupal_get_form("gudong_resume_create_form",$seq);
    $html["#markup"] .= drupal_render($d_form);
    return $html;
}

function gudong_resume_createfail_page() {
    $html = '';
    $html .= '<div>很抱歉，股东数目上限为5，无法继续添加股东了</div>';
    return $html;
}

function create_form_field($engname,$chiname,$type,$formname){
	$field = array("#title" => t($chiname));
	if($type == "str") {
		$field["#type"] = "textfield";
		$field["#required"] = TRUE;
		$field["#prefix"] = "<tr><td>".$chiname."</td><td>";
		$field["#suffix"] = "</td></tr>";
		$field["#default_value"] = isset($_SESSION[$formname][$engname])?$_SESSION[$formname][$engname]:"";
	}elseif($type == "txt") {
		$field["#type"] = "textarea";
		$field["#required"] = FALSE;
		$field["#prefix"] = "<tr><td>".$chiname."</td><td>";
		$field["#suffix"] = "</td></tr>";
		$field["#default_value"] = isset($_SESSION[$formname][$engname])?$_SESSION[$formname][$engname]:"";
	}elseif($type == "num" || $type == "int") {
		$field["#type"] = "textfield";
		$field["#required"] = FALSE;
		$field["#prefix"] = "<tr><td>".$chiname."</td><td>";
		$field["#suffix"] = "</td></tr>";
		$field["#default_value"] = 0;
	}elseif($type == "cbx") {
        $field["#type"] = "checkbox";
        $field["#required"] = FALSE;
        $field["#prefix"] = "<tr><td>".$chiname."</td><td>";
        $field["#suffix"] = "</td></tr>";
        $field["#default_value"] = 0;
    }
	return $field;
}

function fund_co_info_create_page() {
	$html = array("#markup" => "");
	$d_form = drupal_get_form("fund_co_info_create_form");
	$html["#markup"] .= drupal_render($d_form);
	return $html;
}

function fund_co_info_create_form() {
	$fields = load_database_fields("schema3.data");
	$i = 0;
	foreach ($fields as $value) {
		$form[$value["engname"]] = create_form_field($value["engname"],$value["chiname"],$value["type"],"fund_co_info_create_form");
		if ($i == 0)
			$form[$value["engname"]]["#prefix"] = "<table style=\"width:50%\">".$form[$value["engname"]]["#prefix"];
        if ($value["engname"] == "gudong")
            continue;
		if ($i == count($fields)-2)
			$form[$value["engname"]]["#suffix"] = $form[$value["engname"]]["#suffix"]."</table>";
		$i += 1;
	}
    for($i = 0 ; $i < 5 ; $i++){
        $form["gudong".$i."_name"] = array(
            "#type" => "textfield",
            "#prefix" => "<tr><td>".($i+1)."</td><td>",
            "#suffix" => "</td>",
        );
        $form["gudong".$i."_chuzie"] = array(
            "#type" => "textfield",
            "#prefix" => "<td>",
            "#suffix" => "</td>",
        );
        $form["gudong".$i."_chuzibili"] = array(
            "#type" => "textfield",
            "#prefix" => "<td>",
            "#suffix" => "</td><td>".l("增加","manager_resume/gudong/create/".$i)."</td></tr>",
        );
    }
    $form["gudong0_name"]["#prefix"] = "<table><tr><th>序号</th><th>主要股东/合伙人</th><th>出资额(万元)</th>
    <th>出资比例</th><th>操作</th></tr>".$form["gudong0_name"]["#prefix"];
    $form["gudong".($i - 1)."_chuzibili"]["#suffix"] = $form["gudong".($i - 1)."_chuzibili"]["#suffix"]."</table>";
	$form["submit"] = array(
		"#type" => "submit",
		'#value' => t("保存提交"),
	);
	return $form;
}

function fund_co_info_create_form_submit($form,&$form_state) {
	$values = $form_state["values"];
	$fields = load_database_fields("schema3.data");
	global $user;
	$db_fields = array(
		"jijin_id" => get_jijin_id_by_user_id($user->uid),
	);
	foreach ($fields as $i) {
        if($i["engname"] == "gudong") continue;
		$db_fields[$i["engname"]]= $values[$i["engname"]];
	}
    $db_fields["gudong"] = "";
    for($i = 0; $i < 5; $i++) {
        $db_fields["gudong"] .= $values["gudong".$i."_name"]."|";
        $db_fields["gudong"] .= $values["gudong".$i."_chuzie"]."|";
        $db_fields["gudong"] .= $values["gudong".$i."_chuzibili"]."|";
    }
	$last_id = db_insert('manager_resume')->fields($db_fields)->execute();
	if($last_id)
		$form_state["redirect"] = "manager_resume/manage";
	else {
		drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
	}
}

function manager_resume_create_form() {
	$fields = load_database_fields("schema.data");
	$i = 0;
	foreach ($fields as $value) {
		$form[$value["engname"]] = create_form_field($value["engname"],$value["chiname"],$value["type"],"manager_resume_create_form");
		if ($i == 0)
			$form[$value["engname"]]["#prefix"] = "<table style=\"width:60%\">".$form[$value["engname"]]["#prefix"];
		if ($i == count($fields)-1)
			$form[$value["engname"]]["#suffix"] = $form[$value["engname"]]["#suffix"]."</table>";
		$i += 1;
	}
	$form["save"] = array(
		"#type" => "submit",
		'#value' => t("暂存"),
		'#submit' => array('manager_resume_create_form_save'),
	);
	$form["submit"] = array(
		"#type" => "submit",
		'#value' => t("保存提交(提交之后不可修改)"),
	);
	return $form;
}

function manager_resume_create_form_submit($form,&$form_state) {
	$values = $form_state["values"];
	$fields = load_database_fields("schema.data");
	global $user;
	$db_fields = array(
		"jijin_id" => get_jijin_id_by_user_id($user->uid),
		"user_id" => $user->uid,
	);
	foreach ($fields as $i) {
		$db_fields[$i["engname"]]= $values[$i["engname"]];
	}
	$last_id = db_insert('manager_resume')->fields($db_fields)->execute();
	if($last_id)
		$form_state["redirect"] = "manager_resume/manage";
	else {
		drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
	}
}

function manager_resume_edit_page($id){
	$html = array("#markup" => "");
	$html["#markup"] .= l("切换到浏览","manager_resume/view/".$id);
	$d_form = drupal_get_form("manager_resume_edit_form",$id);
	$html["#markup"] .= drupal_render($d_form);
	return $html;
}

function manager_resume_edit_form($form,&$form_state,$id) {
	$field_values = get_resume_by_id($id);
	$fields = load_database_fields("schema.data");
	$i = 0;
	foreach ($fields as $value) {
		if($value["type"] != "txt"){
			$form[$value["engname"]] = array(
				"#type"=>"textfield",
				"#prefix"=>"<tr><td>".$value["chiname"]."</td><td>",
				"#suffix"=>"</td></tr>",
				"#default_value"=>$field_values[$value["engname"]]
			);
		}
		else {
			$form[$value["engname"]] = array(
				"#type"=>"textarea",
				"#prefix"=>"<tr><td>".$value["chiname"]."</td><td>",
				"#suffix"=>"</td></tr>",
				"#default_value"=>$field_values[$value["engname"]]
			);
		}
		if ($i == 0)
			$form[$value["engname"]]["#prefix"] = "<table style=\"width:60%\">".$form[$value["engname"]]["#prefix"];
		if ($i == count($fields)-1)
			$form[$value["engname"]]["#suffix"] = $form[$value["engname"]]["#suffix"]."</table>";
		$i += 1;
	}
	$form['bid'] = array(
		'#type' => 'hidden',
		"#value" => $field_values["id"],
	);
	$form["submit"] = array(
		"#type" => "submit",
		'#value' => t("保存并提交(提交后不能再进行修改)"),
	);
	return $form;
}

function manager_resume_edit_form_submit($form,&$form_state) {
	$values = $form_state["values"];
	$fields = load_database_fields("schema.data");
	global $user;
	$db_fields = array();
	foreach ($fields as $i) {
		$db_fields[$i["engname"]]= $values[$i["engname"]];
	}
	$last_id = db_update('manager_resume')->fields($db_fields)->condition("id",$values["bid"])->execute();
	if($last_id)
		$form_state["redirect"] = "manager_resume/view";
	else {
		drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
	}
}

function manager_resume_view_page($id){
	$field_values = get_resume_by_id($id);
	$html = "";
	if(user_access('manager_resume declare')){
		$html .=   l('编辑', 'manager_resume/edit/'.$id).'<br><br>';
	}
	$html .= "<table>
	<tbody>
	<tr><th>是否驻青人员</th><td>".$field_values["shifouzhuqingrenyuan"]."</td><td></td><td></td></tr>
	<tr><th>姓名</th><td>".$field_values["xingming"]."</td><th>性别</th><td>".$field_values["xingbie"]
		."</td><th>国籍</th><td>".$field_values["guoji"]."</td></tr>
	<tr><th>职务</th><td>".$field_values["zhiwu"]."</td><th colspan='2'>任职时间</th><td colspan='2'>"
		.$field_values["renzhishijian"]."</td></tr>
	<tr><th>学历</th><td>".$field_values["xueli"]."</td><th>专业</th><td>".$field_values["zhuanye"]
		."</td><th>出生年月日</th><td>".$field_values["chushengnianyueri"]."</td></tr>
	<tr><th>家庭住址</th><td colspan='5'>".$field_values["jiatingzhuzhi"]."</td></tr>
	<tr><th>办公电话</th><td>".$field_values["bangongdianhua"]."</td><th>手机</th><td>"
		.$field_values["shouji"]."</td><th>E-mail</th><td>".$field_values["email"]."</td></tr>
	<tr><th>通讯地址</th><td colspan='3'>".$field_values["tongxundizhi"]."</td><th>邮编</th><td>"
		.$field_values["youbian"]."</td></tr>
	<tr><th>受教育及工作经历</th><td colspan='5'>".$field_values["sjyjgzjl"]."</td></tr>
	<tr><th>业绩及所获荣誉</th><td colspan='5'>".$field_values["yjjshry"]."</td></tr>
	<tr><th>兼职情况说明</th><td colspan='5'>".$field_values["jzqksm"]."</td></tr>
	</tbody>
	</table>";
	return $html;
}
function gudong_resume_create_form($form,&$form_state,$gudong_seq) {
    $fields = load_database_fields("schema1.data");
    $i = 0;
    foreach ($fields as $value) {
        $form[$value["engname"]] = create_form_field($value["engname"],$value["chiname"],$value["type"],"gudong_resume_create_form");
        if ($i == 0)
            $form[$value["engname"]]["#prefix"] = "<table style=\"width:60%\">".$form[$value["engname"]]["#prefix"];
        if ($i == count($fields)-1)
            $form[$value["engname"]]["#suffix"] = $form[$value["engname"]]["#suffix"]."</table>";
        $i += 1;
    }
    $form["gudong_seq"] = array(
        "#type" => "hidden",
        "#value" => $gudong_seq,
    );
    $form["save"] = array(
        "#type" => "submit",
        '#value' => t("暂存"),
        '#submit' => array('gudong_resume_create_form_save'),
    );
    $form["submit"] = array(
        "#type" => "submit",
        '#value' => t("保存提交(提交之后不可修改)"),
    );
    return $form;
}

function gudong_resume_create_form_submit($form,&$form_state) {
    $values = $form_state["values"];
    $fields = load_database_fields("schema1.data");
    global $user;
    $db_fields = array(
        "user_id" => $user->uid,
        "jijin_id" => get_jijin_id_by_user_id($user->uid),
    );
    foreach ($fields as $i) {
        $db_fields[$i["engname"]]= $values[$i["engname"]];
    }
    $db_fields["gudong_seq"] = $values["gudong_seq"];
    $last_id = db_insert('gudong_resume')->fields($db_fields)->execute();
    if($last_id)
        $form_state["redirect"] = "manager_resume/manage";
    else {
        drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
    }
}

function gudong_resume_edit_page($id){
    $html = array("#markup" => "");
    $d_form = drupal_get_form("gudong_resume_edit_form",$id);
    $html["#markup"] .= drupal_render($d_form);
    return $html;
}

function gudong_resume_edit_form($form,&$form_state,$id) {
    $field_values = get_gudong_resume_by_id($id);
    $fields = load_database_fields("schema1.data");
    $i = 0;
    foreach ($fields as $value) {
        if($value["type"] != "txt"){
            $form[$value["engname"]] = array(
                "#type"=>"textfield",
                "#prefix"=>"<tr><td>".$value["chiname"]."</td><td>",
                "#suffix"=>"</td></tr>",
                "#default_value"=>$field_values[$value["engname"]]
            );
        }
        else {
            $form[$value["engname"]] = array(
                "#type"=>"textarea",
                "#prefix"=>"<tr><td>".$value["chiname"]."</td><td>",
                "#suffix"=>"</td></tr>",
                "#default_value"=>$field_values[$value["engname"]]
            );
        }
        if ($i == 0)
            $form[$value["engname"]]["#prefix"] = "<table style=\"width:60%\">".$form[$value["engname"]]["#prefix"];
        if ($i == count($fields)-1)
            $form[$value["engname"]]["#suffix"] = $form[$value["engname"]]["#suffix"]."</table>";
        $i += 1;
    }
    $form['bid'] = array(
        '#type' => 'hidden',
        "#value" => $field_values["id"],
    );
    $form["submit"] = array(
        "#type" => "submit",
        '#value' => t("保存并提交(提交后不能再进行修改)"),
    );
    return $form;
}

function gudong_resume_edit_form_submit($form,&$form_state) {
    $values = $form_state["values"];
    $fields = load_database_fields("schema1.data");
    global $user;
    $db_fields = array();
    foreach ($fields as $i) {
        $db_fields[$i["engname"]]= $values[$i["engname"]];
    }
    $last_id = db_update('gudong_resume')->fields($db_fields)->condition("id",$values["bid"])->execute();
    if($last_id)
        $form_state["redirect"] = "gudong_resume/view";
    else {
        drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
    }
}

function gudong_resume_view_page($jijin_id, $gudong_seq){
    $field_values = get_resume_by_jijin_id_and_gudong_seq($jijin_id,$gudong_seq);
    $html = "";
    if(user_access('manager_resume declare')){
        $html .=   l('编辑', 'manager_resume/gudong/edit/'.$field_values["id"]).'<br><br>';
    }
    $html .= "<table>
	<tbody>
	<tr><th>是否驻青人员</th><td>".$field_values["shifouzhuqingrenyuan"]."</td><td></td><td></td></tr>
	<tr><th>姓名</th><td>".$field_values["xingming"]."</td><th>性别</th><td>".$field_values["xingbie"]
        ."</td><th>国籍</th><td>".$field_values["guoji"]."</td></tr>
	<tr><th>职务</th><td>".$field_values["zhiwu"]."</td><th colspan='2'>任职时间</th><td colspan='2'>"
        .$field_values["renzhishijian"]."</td></tr>
	<tr><th>学历</th><td>".$field_values["xueli"]."</td><th>专业</th><td>".$field_values["zhuanye"]
        ."</td><th>出生年月日</th><td>".$field_values["chushengnianyueri"]."</td></tr>
	<tr><th>家庭住址</th><td colspan='5'>".$field_values["jiatingzhuzhi"]."</td></tr>
	<tr><th>办公电话</th><td>".$field_values["bangongdianhua"]."</td><th>手机</th><td>"
        .$field_values["shouji"]."</td><th>E-mail</th><td>".$field_values["email"]."</td></tr>
	<tr><th>通讯地址</th><td colspan='3'>".$field_values["tongxundizhi"]."</td><th>邮编</th><td>"
        .$field_values["youbian"]."</td></tr>
	<tr><th>受教育及工作经历</th><td colspan='5'>".$field_values["sjyjgzjl"]."</td></tr>
	<tr><th>业绩及所获荣誉</th><td colspan='5'>".$field_values["yjjshry"]."</td></tr>
	<tr><th>兼职情况说明</th><td colspan='5'>".$field_values["jzqksm"]."</td></tr>
	</tbody>
	</table>";
    return $html;
}
function manager_resume_viewbyfund_page($fund_id){
	$html = "";
	$query = db_select('manager_resume', 'n')
		->condition("jijin_id",$fund_id)
		->fields('n', array ("id","xingming","zhiwu","shouji"));
	$results = $query->execute();
	$fund_information = get_fund_co_info_by_id($fund_id);
	$html .= "<table><tr><th>基金管理人</th><td>".$fund_information["registered_name"]
		."</td><th>组织形式</th><td>".$fund_information["organization_form"]
		."</td></tr><tr><th>注册资本</th><td>".$fund_information["registered_capital"]
		."</td><td></td><td></td></tr>"
		."<tr><th>实收资本</th><td>".$fund_information["actual_capital"]
		."</td><th>实缴比例</th><td>".$fund_information["actual_radio"]."</td></tr>"
		."<tr><th>成立时间</th><td>".date("Y年m月d日",$fund_information["officaled_date"])
		."</td><th>法人代表</th><td>".$fund_information["corporate_representative"]
		."</td></tr><tr><th>联系人</th><td>".$fund_information["contact"]
		."</td><th>联系电话</th><td>".$fund_information["contact_phone"]
		."</td></tr><tr><th>手机</th><td>".$fund_information["contact_cellphone"]
		."</td><th>传真</th><td>".$fund_information["contact_fax"]
		."</td></tr><tr><th>邮箱</th><td>".$fund_information["contact_email"]
		."</td><th>邮编</th><td>".$fund_information["postcode"]
		."</td></tr><tr><th>注册地址</th><td>".$fund_information["registration_address"]
		."</td><th>办公地址</th><td>".$fund_information["office_address"]
		."</td></tr><tr><th>营业范围</th><td colspan='3'>".$fund_information["scope_of_business"]
		."</td></tr><tr><th colspan='4'>主要管理人员</th></tr>"
		."<tr><th>姓名</th><th>职务</th><th>电话</th><th>简历</th></tr>";
	foreach($results as $manager){
		$html .= "<tr><td>".$manager->xingming
				."</td><td>".$manager->zhiwu
				."</td><td>".$manager->shouji
				."</td><td>".l("查看简历","manager_resume/view/".$manager->id)."</td></tr>";
	}
	$html .= "</table><hr/>";
    $html .= "<table><tr><th colspan='4'>股东、合伙人或实际控制人</th></tr><tr><th>序号</th><th>主要股东/合伙人</th><th>操作</th></tr>";
    $query = db_select('gudong_resume', 'n')
        ->condition("jijin_id",$fund_id)
        ->fields('n', array ("id","xingming","zhiwu","shouji"));
    $results = $query->execute();
    $count = 0;
    foreach($results as $gudong) {
        $count++;
        $html .= "<tr><td>".$count
            ."</td><td>".$gudong->xingming
            ."</td><td>".l("查看简历","manager_resume/gudong/view/".$fund_id."/".($count - 1))."</td></tr>";
    }
    $html .= "</table>";
    #$html .= view_gudong_table($fund_information["gudong"],$fund_id);
	return ($html);
}

function manager_resume_manage_page(){
	global $user;
	$html = "";
    $query = db_select('gudong_resume', 'n')
        ->condition("user_id", $user->uid)
        ->fields('n', array ("id", "gudong_seq"));
    $results = $query->execute();
    $gudong_seqable = array(0, 0, 0, 0, 0);
    foreach ($results as $node) {
        $gudong_seqable[$node->gudong_seq] = 1;
    }
    $gudong_choose = 5;
    for ($i=0; $i<5; $i++) {
        if ($gudong_seqable[$i] == 0) {
            $gudong_choose = $i;
            break;
        }
    }
	$html .=   l('增加管理人信息', 'manager_resume/create').'<br>'.l('增加股东、合伙人或实际控制人', 'manager_resume/gudong/create/'.$gudong_choose).'<br>';
	$html .= manager_resume_viewbyfund_page(get_jijin_id_by_user_id($user->uid));
	$header = array("姓名","职务","手机");
	$query = db_select('manager_resume', 'n')
		->condition("user_id",$user->uid)
		->extend('PagerDefault')
		->limit(10)
		->extend('TableSort')
		->fields('n', array ("id","xingming","zhiwu","shouji"));
	$results = $query->execute();
	$rows = array();
	foreach ($results as $node) {
		$rows[] = array(
			'data' => array(
				l($node->xingming,"manager_resume/view/".$node->id),
				$node->zhiwu,
				$node->shouji,
			)
		);
	}
	/*$html .= theme('table',
		array(
			'header' => $header,
			'rows'=>$rows,
			'sticky' => TRUE,//Optional to indicate whether the table headers should be sticky
			'empty' => '没有记录',//Optional empty text for the table if resultset is empty
		)
	);*/
	$html .= theme('pager',
		array(
			'tags' => array()
		)
	);
	return ($html);
}

function manager_resume_viewfund_page(){
	$html = "";
	$header = array("基金名称");
	$query = db_select('fund_basic_information', 'n')
		->extend('PagerDefault')
		->limit(10)
		->extend('TableSort')
		->fields('n', array ("id","fund_id","registered_name"));
	$results = $query->execute();
	$rows = array();
	foreach ($results as $node) {
		$rows[] = array(
			'data' => array(
				l($node->registered_name, "manager_resume/viewbyfund/".$node->id),
			)
		);
	}
	$html .= theme('table',
		array(
			'header' => $header,
			'rows'=>$rows,
			'sticky' => TRUE,//Optional to indicate whether the table headers should be sticky
			'empty' => '没有记录',//Optional empty text for the table if resultset is empty
		)
	);
	$html .= theme('pager',
		array(
			'tags' => array()
		)
	);
	return ($html);
}

function sub_fund_money_record_create_page(){
	global $user;
	$jijin_id = get_jijin_id_by_user_id($user->uid);
	$html = "";
	$d_form = drupal_get_form("sub_fund_money_record_create_form",$jijin_id);
	$html .= drupal_render($d_form);
	return $html;
}

function sub_fund_money_record_create_form($form,&$form_state,$jijin_id){
	$form['type'] = array(
		'#type' => 'select',
		'#title' => t('明细项目'),
		'#options' => array(
			1 => t('项目投资'),
			2 => t('管理费'),
			3 => t('其他支出'),
			4 => t('注册资金'),
			5 => t('项目退出收入'),
			6 => t('利息'),
			7 => t('其他收入'),
		),
		'#default_value' => 1,
	);
	$form['amount'] = array(
		'#type' => 'textfield',
		'#title' => t('金额'),
		'#default_value' => 0,
	);
	$form['comment'] = array(
		'#type' => 'textarea',
		'#title' => t('备注'),
	);
	$form['jijin_id'] = array(
		'#type' => 'hidden',
		'#value' => $jijin_id,
	);
	$form["submit"] = array(
		"#type" => "submit",
		'#value' => t("增加"),
	);
	return $form;
}

function sub_fund_money_record_create_form_submit($form,&$form_state) {
	$values = $form_state["values"];
	$fields = load_database_fields("schema2.data");
	$db_fields = array();
	foreach ($fields as $i) {
		if($i["engname"] != 'time')
			$db_fields[$i["engname"]]= $values[$i["engname"]];
	}
	$db_fields['jijin_id'] = $values["jijin_id"];
	$db_fields['time'] = time();
	$last_id = db_insert('sub_fund_money')->fields($db_fields)->execute();
	if($last_id)
		$form_state["redirect"] = "manager_resume/sub_fund_money/record_list";
	else {
		drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
	}
}

function sub_fund_money_record_edit_page($record_id){
	$html = "";
	$d_form = drupal_get_form("sub_fund_money_record_edit_form",$record_id);
	$html .= drupal_render($d_form);
	return $html;
}

function sub_fund_money_record_edit_form($form,&$form_state,$record_id){
	$record = get_sub_fund_money_record_by_id($record_id);
	$form['type'] = array(
		'#type' => 'select',
		'#title' => t('明细项目'),
		'#options' => array(
			1 => t('项目投资'),
			2 => t('管理费'),
			3 => t('其他支出'),
			4 => t('注册资金'),
			5 => t('项目退出收入'),
			6 => t('利息'),
			7 => t('其他收入'),
		),
		'#default_value' => $record["type"],
	);
	$form['amount'] = array(
		'#type' => 'textfield',
		'#title' => t('金额'),
		'#default_value' => $record["amount"],
	);
	$form['comment'] = array(
		'#type' => 'textarea',
		'#title' => t('备注'),
		'#value' => $record['comment'],
	);
	$form['record_id'] = array(
		'#type' => 'hidden',
		'#value' => $record_id,
	);
	$form["submit"] = array(
		"#type" => "submit",
		'#value' => t("修改"),
	);
	return $form;
}

function sub_fund_money_record_edit_form_submit($form,&$form_state) {
	$values = $form_state["values"];
	$fields = load_database_fields("schema2.data");
	$db_fields = array();
	foreach ($fields as $i) {
		if($i["engname"] != 'time')
			$db_fields[$i["engname"]]= $values[$i["engname"]];
	}
	$db_fields['time'] = time();
	$last_id = db_update('sub_fund_money')->condition("id",$values["record_id"])->fields($db_fields)->execute();
	if($last_id)
		$form_state["redirect"] = "manager_resume/sub_fund_money/record_list";
	else {
		drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
	}
}

function sub_fund_money_record_list_page(){
	global $user;
	$jijin_id = get_jijin_id_by_user_id($user->uid);
	$html = "";
	$html .= l("添加新记录","manager_resume/sub_fund_money/record_create");
	$header = array("时间","收入/支出","明细项目","金额","备注","操作");
	$query = db_select('sub_fund_money', 'n')
		->condition("jijin_id",$jijin_id)
		->extend('PagerDefault')
		->limit(30)
		->extend('TableSort')
		->fields('n', array ("id","time","type","amount","comment"));
	$results = $query->execute();
	$rows = array();
	foreach ($results as $node) {
		$szmx = type2shouzhimingxi($node->type);
		$rows[] = array(
			'data' => array(
				date("Y年m月d日",$node->time),
				$szmx["shouzhi"],
				$szmx["mingxi"],
				$node->amount,
				$node->comment,
				l("修改","manager_resume/sub_fund_money/record_edit/".$node->id),//TODO: add delete link
			)
		);
	}
	$html .= theme('table',
		array(
			'header' => $header,
			'rows'=>$rows,
			'sticky' => TRUE,//Optional to indicate whether the table headers should be sticky
			'empty' => '没有记录',//Optional empty text for the table if resultset is empty
		)
	);
	$html .= theme('pager',
		array(
			'tags' => array()
		)
	);
	return ($html);
}

function type2shouzhimingxi($type)
{
	$result = array();
	if ((int)$type <= 3)
		$result["shouzhi"] = "收入";
	else
		$result["shouzhi"] = "支出";
	$dict = array(
		1 => t('项目投资'),
		2 => t('管理费'),
		3 => t('其他费用'),
		4 => t('注册资金'),
		5 => t('项目退出收入'),
		6 => t('利息'),
		7 => t('其他收入'),
	);
	$result["mingxi"] = $dict[$type];
	return $result;
}

function sub_fund_money_record_list_check_page($jijin_id){
	$html = get_registered_name_by_id($jijin_id)." 流水账<br>";
	$header = array("时间","收入/支出","明细项目","金额","备注");
	$query = db_select('sub_fund_money', 'n')
		->condition("jijin_id",$jijin_id)
		->extend('PagerDefault')
		->limit(30)
		->extend('TableSort')
		->fields('n', array ("id","time","type","amount","comment"));
	$results = $query->execute();
	$rows = array();
	foreach ($results as $node) {
		$szmx = type2shouzhimingxi($node->type);
		$rows[] = array(
			'data' => array(
				date("Y年M月D日",$node->time),
				$szmx["shouzhi"],
				$szmx["mingxi"],
				$node->amount,
				$node->comment,
			)
		);
	}
	$html .= theme('table',
		array(
			'header' => $header,
			'rows'=>$rows,
			'sticky' => TRUE,//Optional to indicate whether the table headers should be sticky
			'empty' => '没有记录',//Optional empty text for the table if resultset is empty
		)
	);
	$html .= theme('pager',
		array(
			'tags' => array()
		)
	);
	return ($html);
}

function sub_fund_money_stat_page(){
	global $user;
	$jijin_id = get_jijin_id_by_user_id($user->uid);
	$html = "";
	$query = db_select('sub_fund_money', 'n')->fields('n', array ("jijin_id","time","type","amount","comment"));
	$records = $query->execute();
	$query = db_select('fund_basic_information', 'n')->fields('n', array ("id","registered_name","registered_capital"));
	$funds_q = $query->execute();
	$funds = array();
	foreach ($funds_q as $fund){
		$funds[(string)$fund->id] = array(
			"id" => $fund->id,
			"name" => $fund->registered_name,
			"capital" => $fund->registered_capital,
			"calc1" => 0,"calc2" => 0,"calc3" => 0,"calc4" => 0,"calc5" => 0,"calc6" => 0,"calc7" => 0,
			"left" => 0,"in"=>0,"out"=>0,
		);
	}
	$dict_calc = array("left","calc1","calc2","calc3","calc4","calc5","calc6","calc7");

	foreach ($records as $record){
		$funds[(string)$record->jijin_id][$dict_calc[$record->type]] += $record->amount;
		if($record->type <= 3){
			$funds[(string)$record->jijin_id]["left"] -= $record->amount;
			$funds[(string)$record->jijin_id]["out"] += $record->amount;
		}
		else{
			$funds[(string)$record->jijin_id]["left"] += $record->amount;
			$funds[(string)$record->jijin_id]["out"] += $record->amount;
		}
	}

	$sum_c = array("calc1"=>0,"calc2"=>0,"calc3"=>0,"calc4"=>0,"calc5"=>0,"calc6"=>0,"calc7"=>0,"capital"=>0,"left"=>0);
	foreach ($funds as $fund){
		$sum_c["calc1"] += $fund["calc1"];
		$sum_c["calc2"] += $fund["calc2"];
		$sum_c["calc3"] += $fund["calc3"];
		$sum_c["calc4"] += $fund["calc4"];
		$sum_c["calc5"] += $fund["calc5"];
		$sum_c["calc6"] += $fund["calc6"];
		$sum_c["calc7"] += $fund["calc7"];
		$sum_c["capital"] += $fund["capital"];
		$sum_c["left"] += $fund["left"];
	}

	$html .= "<table><tr><th rowspan='2'>序号</th><th rowspan='2'>基金注册名称</th><th rowspan='2'>注册资本</th> <th colspan='3'>支出</th>
	<th colspan='4'>收入</th><th rowspan='2'>账户余额</th><th rowspan='2'>操作</th> </tr><tr><th>已投资额</th>
	<th>管理费</th><th>其他费用</th><th>实缴资本</th><th>项目退出收入</th><th>利息</th><th>其他收入</th></tr>";
	$i = 0;
	foreach($funds as $fund){
		$i += 1;
		$html .= "<tr><td>".$i."</td><td>".$fund["name"]."</td><td>"
			.$fund["capital"]."</td><td>".$fund["calc1"]."</td><td>".$fund["calc2"]."</td><td>".$fund["calc3"]."</td><td>"
			.$fund["calc4"]."</td><td>".$fund["calc5"]."</td><td>".$fund["calc6"]."</td><td>".$fund["calc7"]."</td><td>"
			.$fund["left"]."</td><td>".l("查看","manager_resume/sub_fund_money/record_list_check/".$fund["id"])."</td></tr>";
	}
	$html .= "<tr><td>合计</td><td></td><td>"
		.$sum_c["capital"]."</td><td>".$sum_c["calc1"]."</td><td>".$sum_c["calc2"]."</td><td>".$sum_c["calc3"]."</td><td>"
		.$sum_c["calc4"]."</td><td>".$sum_c["calc5"]."</td><td>".$sum_c["calc6"]."</td><td>".$sum_c["calc7"]."</td><td>"
		.$sum_c["left"]."</td><td></td></tr></table>";
	return ($html);
}

function view_gudong_table($gudongstr,$fund_id){
    $gudongs= explode("|",$gudongstr);
    $length = count($gudongs)/ 3;
    if($gudongstr=="") $length = 0;
    $html = "<table><tr><th>序号</th><th>主要股东/合伙人</th><th>出资额(万元)</th>
    <th>出资比例</th><th>操作</th></tr>";
    for($i = 0; $i < $length; $i++) {
	    $html .= "<tr><td>".($i+1)."</td><td>".$gudongs[$i*3]."</td><td>
    	".$gudongs[$i * 3 + 1]."</td><td>".$gudongs[$i*3 + 2]."</td><td>".l("查看","manager_resume/gudong/view/".$fund_id."/".$i)."</td></tr>";
	}
    $html .= "</table>";
    return $html;
}
function edit_gudong_table($fund_id){
    return "";
}